﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.Services.Contracts.DTO.Infos;
using System.Data;
using Demo.CommonBase.Logging;
using System.Reflection;
using Demo.CommonBase.Exceptions;

namespace Demo.Services.Integration.DAO.IntegrationComponents.Mappings
{
    class ResourceTypeDAOMapper
    {
        public static ResourceTypeResultInfo GetResourceTypeList(DataSet ds)
        {
            ResourceTypeResultInfo result;
            List<ResourceTypeInfo> resourceTypeList;
            try
            {
                Logger.Info(MethodInfo.GetCurrentMethod().ToString());
                DataTable dt = ds.Tables[1];

                resourceTypeList = (from resourceType in dt.AsEnumerable()
                          select new ResourceTypeInfo
                          {
                              Id = resourceType.Field<int>("id"),
                              Description = resourceType.Field<string>("descripcion"),
                              Cost = resourceType.Field<decimal>("costo")
                          }).ToList();
                result = new ResourceTypeResultInfo();
                result.ResourceTypeList = resourceTypeList;
                result.TotalReg = Convert.ToInt32(ds.Tables[0].Rows[0]["TotalReg"]);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;
        }
        public static List<ResourceTypeInfo> GetList(DataSet ds)
        {

            List<ResourceTypeInfo> resourceTypeList;
            try
            {
                DataTable dt = ds.Tables[0];
                resourceTypeList = (from resourceType in dt.AsEnumerable()
                                    select new ResourceTypeInfo
                                    {
                                        Id = resourceType.Field<int>("id"),
                                        Description = resourceType.Field<string>("descripcion"),
                                        Cost = resourceType.Field<decimal>("costo")
                                    }).ToList();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return resourceTypeList;
        }
        public static ResourceTypeInfo GetById(DataSet ds)        
        {            
            ResourceTypeInfo result;            
            try            
            {                
                Logger.Info(MethodInfo.GetCurrentMethod().ToString());                
                DataTable dt = ds.Tables[0];                
                result = (from resourceType in dt.AsEnumerable()                         
                          select new ResourceTypeInfo                         
                          {                              
                              Id = resourceType.Field<int>("id"),                              
                              Description = resourceType.Field<string>("descripcion"),                              
                              Cost = resourceType.Field<decimal>("costo")                          
                          }).ToList()[0];            
            }            
            catch (Exception ex)            
            {                
                Logger.Error(ex);                
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);            
            }            
            return result;        
        }    
    }
}
